import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../src/login.vue'
import home from '../src/home.vue'

Vue.use(VueRouter)

const router = new VueRouter({
	routes: [
		// {path:'/',redirect:'/login'},
		{path:'/login',component:Login},
		{
			path:'/home',
			component:home,
			redirect:'/main',
			children:[
				{
					path:'/main',
					name:'main',
					component:() => import('../view/home/index.vue'),
				
				},
				{
					path:'/users',
					name:'users',
					component:() => import('../view/user/index.vue')
				},
				{
					path:'/comment1',
					name:'comment1',
					component:() => import('../view/comment/commentOne.vue')
				},
				{
					path:'/comment2',
					name:'comment2',
					component:() => import('../view/comment/commentTwo.vue')
				},
				{
					path:'/page1',
					name:'page1',
					component:() => import('../view/others/otherOne.vue')
				},
				{
					path:'/menus',
					name:'menu',
					component:() => import('../view/menu/index.vue')
				}
				
			]
		},
		
	]
})

// 设置路由导航守卫
router.beforeEach((to,from,next) => {
	if(to.path === '/login') return next();
	// 获取token
	const tokenStr = window.localStorage.getItem('token')
	if(!tokenStr) return next('/login')
	next()
})

export default router
